<?php

class Core_Component_System_Model_Dms_Property extends Soul_Model_Entity_Base {
	
	protected $tableName = '{t}system_dms_property';
    
    public function getEntity(){
        if(!$this->issetData('entity')){
            if($this->issetData('entity_id')){
                $this->data['entity'] = App::loadModel('system/dms_entity', $this->getEntityId());
            }
        }
        return $this->data['entity'];
    }
    
    protected function beforeInsert() {
        if(!$this->issetData('sort')){
            $sort = $GLOBALS[RC]->getQuery()
                                ->reset()
                                ->from($this->tableName)
                                ->select('MAX(sort)')
                                ->condition('entity_id', $this->getEntityId())
                                ->fetch()
                                ->getFirstValue();
            $this->setSort($sort + 1);
        }
        parent::beforeInsert();
    }
    
    protected function afterInsert() {
        App::getBusiness('system/dms_entity')->createField($this);        
        parent::afterInsert();
    }

    protected function initializeProperties() {
        $this->createProperty('id', 'integer', TRUE, TRUE);
        $this->createProperty('entity_id', 'integer')->setUpdateable(FALSE);
        $this->createProperty('name', 'string');
        $this->createProperty('property_type', 'string');
        $this->createProperty('field_type', 'string');
        $this->createProperty('length', 'string');
        $this->createProperty('required', 'boolean');
        $this->createProperty('is_primary', 'boolean');
        $this->createProperty('is_increment', 'boolean');
        $this->createProperty('insertable', 'boolean');
        $this->createProperty('updateable', 'boolean');
        $this->createProperty('versionable', 'boolean')->setUpdateable(FALSE);
        $this->createProperty('default_value', 'string');
        $this->createProperty('input_type', 'string');
        $this->createProperty('item_count', 'integer');
        $this->createProperty('label', 'string');
        $this->createProperty('description', 'string');
        $this->createProperty('sort', 'integer');
        $this->createProperty('timestamp_action', 'string');
    }

}